package com.easidea.tim.service.finance;

import java.util.Date;
import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.criterion.MatchMode;
import org.hibernate.criterion.Projections;
import org.hibernate.criterion.Restrictions;
import org.springframework.dao.DataAccessException;
import org.springside.core.service.EntityService;

import com.easidea.tim.dao.customer.CustomerDao;
import com.easidea.tim.dao.finance.DueFromDao;
import com.easidea.tim.dao.finance.DueFromPayedRecordDao;
import com.easidea.tim.dao.finance.DueFromSummaryDao;
import com.easidea.tim.dao.saleorder.SaleOrderDao;
import com.easidea.tim.dao.system.orgnization.StaffDAO;
import com.easidea.tim.entity.customer.Customer;
import com.easidea.tim.entity.finance.DueFrom;
import com.easidea.tim.entity.finance.DueFromPayedRecord;
import com.easidea.tim.entity.finance.DueFromSummary;
import com.easidea.tim.entity.finance.ShouldPaySummary;
import com.easidea.tim.entity.system.orgnization.Staff;
import com.easidea.tim.util.DateUtil;
import com.easidea.tim.util.Page;

public class DueFromPayedRecordService extends EntityService<DueFromPayedRecord, DueFromPayedRecordDao>{
  private DueFromPayedRecordDao dueFromPayedRecordDao;
  private SaleOrderDao saleOrderDao;
  private DueFromDao dueFromDao;
  private DueFromSummaryDao  dueFromSummaryDao;
  private CustomerDao customerDao;
  private StaffDAO staffDao;
  
public Page getDueFromPayedRecordListByCondition(Long customerId, String customerName, 
		  Date startDate,Date endDate,Long orderId,Page page) {
		Criteria ca = this.getDueFromPayedRecordListByConditionCriteria(
				customerId, customerName, startDate, endDate, orderId);
		ca.setProjection(Projections.count("id"));
		Integer totalNum = (Integer) ca.uniqueResult();
		Criteria ca1 = this.getDueFromPayedRecordListByConditionCriteria(
				customerId, customerName, startDate, endDate, orderId);
		int startIndex = Page.getStartOfPage(page.getCurrentPageNo(), page.getPageSize());
		ca1.setFirstResult(startIndex);
		ca1.setMaxResults(page.getPageSize());
		List<DueFromPayedRecord> ls = (List<DueFromPayedRecord>) ca1.list();
		Page resultPage = new Page(startIndex, totalNum, page.getPageSize(), ls);
		resultPage.setCurrentPageNo(page.getCurrentPageNo());
		
		return resultPage;
	}
	private Criteria getDueFromPayedRecordListByConditionCriteria(Long customerId,
			String customerName, 
			  Date startDate,Date endDate,Long orderId) {
		Criteria ca = this.createUnDeletedCriteria();
		ca.createAlias("order", "order");
		ca.createAlias("order.customer", "customer");		
		if(startDate != null && !startDate.equals("") && endDate != null && !endDate.equals("") ) {
			ca.add(Restrictions.between("payDate", DateUtil.getStartTime(startDate), DateUtil.getEndTime(endDate)));
		}
		if (orderId != -1) {
			ca.add(Restrictions.eq("order.id", orderId));
		}
		if (customerName != null && !customerName.equals("")) {
			ca.add(Restrictions.ilike("customer.name", customerName, MatchMode.START));
		}
		if(customerId != -1) {
			ca.add(Restrictions.eq("customer.id", customerId));
		}
		return ca;
	}
	public Page getDueFromSummarByCustomer(long customerId,
			String responer, Date startDate,Date endDate,Page page) { 
		return this.dueFromSummaryDao.getDueFromSummarByCustomer(customerId, responer,startDate,endDate, page);
	}
	public Page getDueFromSummarBySalesStaff(String salesStaff, Date startDate,Date endDate,Page page) { 
		return this.dueFromSummaryDao.getDueFromSummarBySalesStaff(salesStaff, startDate, endDate, page);
	}	
	 /**
	  *获取销售订单流水号和Id 
	  * @return
	  */
	public List<Object[]> getAllSaleOrderList(){
		return this.saleOrderDao.getAllFlowNos();
	}	
	public String savePay(DueFromPayedRecord pay) {
		try{
		
		 DueFrom dueFrom = this.dueFromDao.getDueFromBySalesOrderId(pay.getOrder().getId());
		 DueFromSummary paySummary = dueFrom.getDueFromSummary();
		 pay.setPayDate(new Date());
		 pay.setDueFrom(dueFrom);
		 this.dueFromPayedRecordDao.getHibernateTemplate().save(pay);
		 paySummary.setPayed(paySummary.getPayed() + pay.getAmount());
		 if (paySummary.getAmount() == paySummary.getPayed()) {
			 paySummary.setCleared(ShouldPaySummary.PAY_CLEARED_YES);
		 }
		this.dueFromSummaryDao.getHibernateTemplate().update(paySummary);
		}catch(DataAccessException de) {
			de.printStackTrace();
			return de.getMessage();
		}
		return "success";		
	}	
	public List<Customer> filterCustomerByName(String name) {
		return this.customerDao.filterCustomerByName(name);
	}
	public List<Staff> getStaffListbyName(String name) {
		return this.staffDao.getStaffListbyName(name);
	}	
@Override
public DueFromPayedRecordDao getEntityDao() {
	return dueFromPayedRecordDao;
}

public void setDueFromPayedRecordDao(DueFromPayedRecordDao dueFromPayedRecordDao) {
	this.dueFromPayedRecordDao = dueFromPayedRecordDao;
}

public DueFromPayedRecordDao getDueFromPayedRecordDao() {
	return dueFromPayedRecordDao;
}
public SaleOrderDao getSaleOrderDao() {
	return saleOrderDao;
}
public void setSaleOrderDao(SaleOrderDao saleOrderDao) {
	this.saleOrderDao = saleOrderDao;
}
public DueFromDao getDueFromDao() {
	return dueFromDao;
}
public void setDueFromDao(DueFromDao dueFromDao) {
	this.dueFromDao = dueFromDao;
}
public DueFromSummaryDao getDueFromSummaryDao() {
	return dueFromSummaryDao;
}
public void setDueFromSummaryDao(DueFromSummaryDao dueFromSummaryDao) {
	this.dueFromSummaryDao = dueFromSummaryDao;
}
public CustomerDao getCustomerDao() {
	return customerDao;
}
public void setCustomerDao(CustomerDao customerDao) {
	this.customerDao = customerDao;
}
public StaffDAO getStaffDao() {
	return staffDao;
}
public void setStaffDao(StaffDAO staffDao) {
	this.staffDao = staffDao;
}

}
